package com.e3.sysmg.security.server;

import java.util.ArrayList;
import java.util.List;

import com.e3.core.base.ServiceException;
import com.e3.core.base.ServiceLocator;
import com.e3.sysmg.security.client.ModuleHelper;
import com.e3.sysmg.security.client.ModuleRPCService;
import com.e3.sysmg.security.model.ModuleModel;
import com.e3.sysmg.security.model.ModuleQC;
import com.e3.sysmg.security.service.SecurityQueryService;
import com.e3.sysmg.security.service.SecurityService;
import com.e3.web.core.client.WebException;
import com.e3.web.core.server.GwtRPCAction;
import com.e3.web.core.server.GwtRPCExecutor;
import com.e3.web.core.server.GwtRPCVoidExecutor;

public class ModuleRPCServiceImpl extends GwtRPCAction implements ModuleRPCService{

	private SecurityService secService;
	private SecurityQueryService secQueryService;
	
	private SecurityService getSecService() {
		if(secService == null){
			secService = ServiceLocator.getService(SecurityService.class);
		}
		return secService;
	}

	private SecurityQueryService getSecQueryService() {
		if(secQueryService == null){
			secQueryService = ServiceLocator.getQueryService(SecurityQueryService.class);
		}
		return secQueryService;
	}

	public ModuleModel newModule(final ModuleModel moduleModel) throws WebException {
		return new GwtRPCExecutor<ModuleModel>() {
			@Override
			public ModuleModel execute() throws ServiceException, Exception {
				return getSecService().newModule(moduleModel);
			}
		}.doExecute();
	
	}

	public List<ModuleModel> getModulesByCondition(final ModuleQC moduleQC) throws WebException {
		return new GwtRPCExecutor<List<ModuleModel>>() {
			@Override
			public List<ModuleModel> execute() throws ServiceException, Exception {
				List<ModuleModel> modules = getSecQueryService().getModuleByCondition(moduleQC);
				if(modules==null||modules.isEmpty()){
					return new ArrayList<ModuleModel>(0);
				}
				//找出需要补充上级模块的所有模块及上级模块的编码
				List<Long> pIds = new ArrayList<Long>(modules.size());
				for (ModuleModel module : modules) {
					if(!ModuleHelper.INSTANCE.isTopModel(module)){
						Long pId = module.getParentId();
						if(!pIds.contains(pId)){
							pIds.add(pId);
						}
					}
				}
				Long[] ids = new Long[pIds.size()];
				pIds.toArray(ids);
				List<ModuleModel> parentModules = getSecQueryService().getModuleModelByIds(ids);
				//设置查询结果中各模块的上级模块
				for (ModuleModel module : modules) {
					Long pID = module.getParentId();
					for (ModuleModel moduleModel : parentModules) {
						Long id = moduleModel.getId();
						if(pID.equals(id)){
							module.setParentModel(moduleModel);
							break;
						}
					}
					
				}
				return modules;
			}
		}.doExecute();
	}

	public ModuleModel modifyModule(final ModuleModel moduleModel) throws WebException {
		return new GwtRPCExecutor<ModuleModel>() {
			@Override
			public ModuleModel execute() throws ServiceException, Exception {
				return getSecService().modifyModule(moduleModel);
			}
		}.doExecute();
	}

	public void deleteModules(final List<ModuleModel> moduleModels) throws WebException {
		new GwtRPCVoidExecutor() {
			@Override
			public void execute() throws ServiceException, Exception {
				getSecService().deleteModules(moduleModels);				
			}
		}.doExecute();
	}

	/**
	 * 
	 */
	public List<ModuleModel> getModuleChildren(final ModuleModel moduleModel) throws WebException {
		return new GwtRPCExecutor<List<ModuleModel>>() {
			@Override
			public List<ModuleModel> execute() throws ServiceException, Exception {
				List<ModuleModel> models = new ArrayList<ModuleModel>();
				if(moduleModel == null){
					ModuleModel model = ModuleHelper.INSTANCE.createRootModel();
					models.add(model);
					return models;
				}
				
				ModuleModel qModel = moduleModel;
				if(ModuleHelper.INSTANCE.isRootModel(qModel)){
					qModel = null;
				}
				
				models = getSecQueryService().getModuleChildren(qModel);
				return models;
			}
		}.doExecute();
	}

	public ModuleModel getModuleByCode(final String code) throws WebException {
		return new GwtRPCExecutor<ModuleModel>() {
			@Override
			public ModuleModel execute() throws ServiceException, Exception {
				return getSecQueryService().getModuleModelByCode(code);
			}
		}.doExecute();
	}

	public ModuleModel getModuleById(final String id) throws WebException {
		return new GwtRPCExecutor<ModuleModel>() {
			@Override
			public ModuleModel execute() throws ServiceException, Exception {
				return getSecQueryService().getModuleModelById(Long.parseLong(id));
			}
		}.doExecute();
		
	}

}
